import { constantRoutes } from '@/router'
import { getMyRoutes } from '@/api/role'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  // 从后台获取菜单
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      getMyRoutes().then(response => {
        var accessedRoutes = JSON.parse(response.data)
        if (response.data === null) {
          accessedRoutes = []
        }
        console.log('菜单：', accessedRoutes)
        replaceComponent(accessedRoutes)
        commit('SET_ROUTES', accessedRoutes)
        resolve(accessedRoutes)
      })
    })
  }
}

function replaceComponent(data) {
  if (data !== undefined && data !== null && data.length !== 0) {
    for (var i = 0; i < data.length; i++) {
      data[i].component = map[data[i].component]
      replaceComponent(data[i].children)
    }
  }
}

const map = {
  layout: () => import('@/layout'),
  complexTable: () => import('@/views/table/complex-table'),
  article: () => import('@/views/table/article'),
  news: () => import('@/views/table/news/list'),
  editNews: () => import('@/views/table/news/edit'),
  createNews: () => import('@/views/table/news/create'),
  login: () => import('@/views/login/index'),
  dashboard: () => import('@/views/dashboard/index'),
  profile: () => import('@/views/profile/index'),
  role: () => import('@/views/permission/role'),
  401: () => import('@/views/error-page/401'),
  managerList: () => import('@/views/manager/list'),
  editManager: () => import('@/views/manager/edit'),
  member: () => import('@/views/user/member/list'),
  attributes: () => import('@/views/attributes/list'),
  spu: () => import('@/views/spu/list'),
  createSpu: () => import('@/views/spu/create'),
  editSpu: () => import('@/views/spu/edit'),
  skuList: () => import('@/views/sku/list'),
  createSku: () => import('@/views/sku/create'),
  editSku: () => import('@/views/sku/edit'),
  order: () => import('@/views/order/list'),
  orderDetail: () => import('@/views/order/detail'),
  catalogList: () => import('@/views/catalog/list'),
  freight: () => import('@/views/freight/setting'),
  themeList: () => import('@/views/theme/list'),
  editTheme: () => import('@/views/theme/edit'),
  createTheme: () => import('@/views/theme/create'),
  messageList: () => import('@/views/messageBox/list'),
  messageDetail: () => import('@/views/messageBox/detail'),
  messageDetail: () => import('@/views/messageBox/detail')
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
